﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {

        Calculate ca;
        public Form1()
        {
            InitializeComponent();
            ca = new Calculate();
            button1.Text = "Calculate";
            label1.Text = "Maximum: ";
            label2.Text = "Minimum: ";
            label3.Text = "Sum: ";
            label4.Text = "Average: ";
            label5.Text = "Input an array:";
            label6.Text = "Input the bundaries of the prime numbers:";
            label7.Text = "Minimum: ";
            label8.Text = "Maximum: ";
            label9.Text = "prime numbers:";
            button2.Text = "start";
        }
        public int[] getArray()
        {
            int capacity = 64;
            int[] arr=new int[capacity];
            int top = -1;
            String s = textBox1.Text;
            bool isPreNum = false;
            int num = 0;
            for(int i = 0; i <= s.Length; i++)
            {
                if (i < s.Length && (int)s[i] >= 48 && (int)s[i] <= 57) 
                {
                    num = num * 10 + (int)s[i] - 48;
                    isPreNum = true;
                }
                else
                {
                    if(isPreNum)
                        arr[++top]=num;
                    num = 0;
                    isPreNum = false;
                }
            }
            int []arrp = new int[top + 1];
            for(int i = 0; i <= top; i++)
            {
                arrp[i] = arr[i];
            }
            return arrp;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int[] numArr = getArray();
            int max = ca.getMax(numArr);
            int min = ca.getMin(numArr);
            int sum = ca.getSum(numArr);
            double average = ca.getAverage(numArr);
            label1.Text = "Maximum: "+max;
            label2.Text = "Minimun: "+min;
            label3.Text = "Sum: "+sum;
            label4.Text = "Average: "+average;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int bMin = int.Parse(textBox3.Text);
            int bMax = int.Parse(textBox4.Text);
            textBox2.Text=ca.print_10(ca.getPrime(bMin, bMax),true);
        }

    }

    public class Calculate
    {
        public String print_10(int[] a, bool doSkipZero)
        {
            String result = "";
            int length = 0;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] <= 0 && doSkipZero)
                    continue;
                result+=("" + a[i] + "  ");
                length++;
                if (length % 10 == 0)
                    result+="\r\n";
            }
            return result;
        }

        public int[] getPrime(int bundaryMin, int bundaryMax)
        {
            int capacity = 100;
            int[] result = new int[capacity];
            int top = -1;
            for (int i = bundaryMin; i < bundaryMax; i++)
            {
                if (top >= capacity - 1)
                {
                    int[] p = new int[2 * capacity];
                    for (int j = 0; j < top; j++)
                    {
                        p[j] = result[j];
                    }
                    result = p;
                    capacity *= 2;
                }
                for (int j = 2; j <= i / 2; j++)
                {
                    if (i % j == 0)
                        goto z;
                }
                if (i >= 2)
                    result[++top] = i;
                z: continue;
            }
            return result;
        }

        public int getMax(int[] a)
        {
            int max = int.MinValue;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] > max)
                    max = a[i];
            }
            return max;
        }

        public int getMin(int[] a)
        {
            int min = int.MaxValue;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] < min)
                    min = a[i];
            }
            return min;
        }

        public int getSum(int[] a)
        {
            int sum = 0;
            for (int i = 0; i < a.Length; i++)
            {
                sum += a[i];
            }
            return sum;
        }

        public double getAverage(int[] a)
        {
            return (double)getSum(a) / a.Length;
        }

    }
}
